﻿namespace DimensionsHelper.LanguageService.Symbols;

public class DeclaredSymbolTable : SymbolTable
{
    protected override Dictionary<string, ISymbol> Symbols { get; } = new(StringComparer.OrdinalIgnoreCase);


    protected override ISymbol? LookupSymbolDeclareCore(string[] qualifiedName)
    {
        // 优先查找引用模块。
        ISymbol? symbol = LookupSymbolInImportedModuleCore(qualifiedName);

        if (symbol != null)
        {
            return symbol;
        }

        // 查找本地符号
        foreach (string? name in qualifiedName)
        {
            symbol = symbol switch
            {
                null => Symbols.GetValueOrDefault(name),
                { Kind: SymbolKinds.Module } => ((ModuleSymbol)symbol).LookupNamedMember(name),
                { Kind: SymbolKinds.NamedType } => ((NamedTypeSymbol)symbol).LookupNamedMember(name),
                _ => symbol
            };

            // 遇到未找到的限定名，可直接返回空
            if (symbol == null)
            {
                return null;
            }
        }

        return symbol;
    }
}